//
//  PhotoBrowserVC.m
//  PickerManager
//
//  Created by iOS开发 on 2018/8/15.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "PhotoBrowserVC.h"
#import "PhotoBrowserAnimator.h"
#import "BrowseImageScrollView.h"

@interface PhotoBrowserVC ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) NSMutableArray *photos;
@property (nonatomic, assign) NSInteger currentItem;
@property (nonatomic, strong) PhotoBrowserAnimator *animator;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@end

@implementation PhotoBrowserVC

- (instancetype)initWithPhotos:(NSArray *)photos andIndex:(NSInteger)indexItem animator:(PhotoBrowserAnimator *)animator {
    self = [super init];
    if (self) {
        _animator = animator;
        _currentItem = indexItem;
        _photos = [NSMutableArray arrayWithArray:photos];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    [self createScrollView];
    [self createDeleteButton];
}

- (void)createDeleteButton {
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    deleteButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    deleteButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 80, 20, 80, 40);
    [self.view addSubview:deleteButton];
}

- (void)createScrollView {
    _scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
    _scroll.pagingEnabled = YES;
    _scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * _photos.count, [UIScreen mainScreen].bounds.size.height);
    _scroll.delegate = self;
    [self.view addSubview:_scroll];
    _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pop)];
    [self.view addGestureRecognizer:_tap];
    
    [_photos enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL * _Nonnull stop) {
        //计算imageView的frame
        CGFloat x = idx * [UIScreen mainScreen].bounds.size.width;
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        CGFloat height = [UIScreen mainScreen].bounds.size.height;
        CGFloat y = 0;
        
        BrowseImageScrollView *imageView = [[BrowseImageScrollView alloc] initWithFrame:CGRectMake(x, y, width, height) WithImage:image];
        imageView.tag = 10000 + idx;
        imageView.hiddtap = self.tap;
        [self.scroll addSubview:imageView];
    }];

    [_scroll setContentOffset:CGPointMake(_currentItem * [UIScreen mainScreen].bounds.size.width, 0) animated:NO];
}

#pragma mark - scrollDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat round = roundf(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
    [self.animator changeEndIndex:round];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    CGFloat round = roundf(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
    BrowseImageScrollView *imageScrollView = [scrollView viewWithTag:10000 + round];
    [imageScrollView setZoomScale:1 animated:YES];
}

// 删除
- (void)deleteButtonAction:(UIButton *)button {
    CGFloat round = roundf(self.scroll.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
    if (self.block) {
        self.block(round);
    }
    _animator.isDeletePop = YES;
    [self pop];
}

// 返回
- (void)pop {
    [self dismissViewControllerAnimated:YES completion:^{
        self.animator.isDeletePop = NO;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
